package com.xmu.prcs.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.xmu.prcs.action.bean.FeatureBean;
import com.xmu.prcs.action.bean.RetrievalResultBean;
import com.xmu.prcs.service.RetrievalManager;

/**
 * 植物检索
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
@SuppressWarnings("serial")
public class RetrievalAction extends BaseAction{

	private RetrievalManager retrievalManager;

	/**
	 * 页面传进来的选中的复选框的value
	 */
	private List<String> checkbox;
	
	/**
	 * 全部性状id集，
	 */
	private List<String> featureId;
	
	/**
	 * 性状重要性得分
	 */
	private List<String> grade;
	
	/**
	 * 传到页面的性状
	 */
	private List<FeatureBean> features;
	
	/**
	 * 植物名
	 */
	private String plantName;
	
	/**
	 * 植物ID
	 */
	private int plantId;
	
	/**
	 * 数值型性状Id
	 */
	private List<String> numberFeature;
	
	/**
	 * 数值型数据
	 */
	private List<String> number;
	
	/**
	 * 植物形状的xml树形式的字符串
	 */
	private String featureXMLStringTree;
	
	private List<RetrievalResultBean> results;
	
	@JSON(serialize=false)
	public RetrievalManager getRetrievalManager() {
		return retrievalManager;
	}

	public void setRetrievalManager(RetrievalManager retrievalManager) {
		this.retrievalManager = retrievalManager;
	}
	
	@JSON(serialize=false)
	public List<String> getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox = checkbox;
	}

	public List<FeatureBean> getFeatures() {
		return features;
	}

	public void setFeatures(List<FeatureBean> features) {
		this.features = features;
	}

	public List<String> getFeatureId() {
		return featureId;
	}

	public void setFeatureId(List<String> featureId) {
		this.featureId = featureId;
	}

	public List<String> getGrade() {
		return grade;
	}

	public void setGrade(List<String> grade) {
		this.grade = grade;
	}

	public String getPlantName() {
		return plantName;
	}

	public void setPlantName(String plantName) {
		this.plantName = plantName;
	}

	public int getPlantId() {
		return plantId;
	}

	public void setPlantId(int plantId) {
		this.plantId = plantId;
	}
	
	public List<String> getNumberFeature() {
		return numberFeature;
	}

	public void setNumberFeature(List<String> numberFeature) {
		this.numberFeature = numberFeature;
	}

	public List<String> getNumber() {
		return number;
	}

	public void setNumber(List<String> number) {
		this.number = number;
	}

	public List<RetrievalResultBean> getResults() {
		return results;
	}

	public void setResults(List<RetrievalResultBean> results) {
		this.results = results;
	}

	public String getFeatureXMLStringTree() {
		return featureXMLStringTree;
	}

	public void setFeatureXMLStringTree(String featureXMLStringTree) {
		this.featureXMLStringTree = featureXMLStringTree;
	}

	/**
	 * 跳转到多道检索页面
	 * @return
	 */
	public String toMultiChannelRetrieval(){
		featureXMLStringTree = retrievalManager.getPlantFeatures4RetrievalOfXMLString();
		return SUCCESS;
	}
	
	/**
	 * 多道检索
	 * @return
	 */
	public String multiChannelRetrieval(){
//		for(int i = 0 ;i< checkbox.size();i++){
//			System.out.println("the checkbox " + i + " is " + checkbox.get(i));
//		}
		if(featureId != null && featureId.size() !=0){
			for(String id : featureId){
				System.out.println("featureId :" + id);
			}
		}
		results = retrievalManager.listMultiChannelRetrieval(featureId, number, numberFeature);
		return "multiChannelRetrieval";
	}
	
	/**
	 * 跳转到为植物增加形状的页面
	 * @return
	 */
	public String toAddFeature(){
		
		features = new ArrayList<FeatureBean>();
		features = retrievalManager.getMultiFeature();
		plantId = this.plantId;
		System.out.println("plantId:" + plantId);
		return SUCCESS;
	}
	
	/**
	 * 为植物增加性状
	 * @return
	 */
	public String addFeature(){
		List<FeatureBean> plantFeatureList = new ArrayList<FeatureBean>();
		FeatureBean plantFeature;
		int index = 0;
		System.out.println("plantId:" + plantId);
		System.out.println("checked number:" + checkbox.size());
		System.out.println("grade number:" + grade.size());
		System.out.println("featureId number:" + featureId.size());
		if(checkbox != null && checkbox.size() != 0){
			for(String fid : checkbox){
				plantFeature = new FeatureBean();
				plantFeature.setIdLevel(Integer.parseInt(fid));
				for( ; index < featureId.size(); index++){
					if(featureId.get(index).equals(fid)){
						break;
					}
				}
				if(grade.get(index).equals("")){
					plantFeature.setGrade(0);
				}else{
					plantFeature.setGrade(Integer.parseInt(grade.get(index)));
				}
				plantFeatureList.add(plantFeature);
			}
		}
		retrievalManager.addFeature4Plant(plantId, plantFeatureList);
		return SUCCESS;
	}
}
